﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace LenderClearingHouse
{
    public partial class RehabEditControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                SetSumma(edRehabEstimate.Value, edMiscCosts.Value);
        }

        protected void FillRehabStatus()
        {
          edRehabStatus.Items.Clear();
          edRehabStatus.Items.Add(new Telerik.Web.UI.RadComboBoxItem("(Select Rehab Status)", "(Select Rehab Status)"));
          DataTable dt = new DataAccess().GetDataTable("getRehabStatusCodes");

          foreach(DataRow dr in dt.Rows)
            edRehabStatus.Items.Add(new Telerik.Web.UI.RadComboBoxItem(dr["Code"].ToString(), dr["Code"].ToString()));
        }

        protected void FillPM()
        {
          edProjectManager.Items.Clear();
          edProjectManager.Items.Add(new Telerik.Web.UI.RadComboBoxItem("(Select GC)", "-1"));
          DataTable dt = new DataAccess().GetDataTable("getProjectManagers");

          foreach (DataRow dr in dt.Rows)
            edProjectManager.Items.Add(new Telerik.Web.UI.RadComboBoxItem(dr["NameProjectManager"].ToString(), dr["UserId"].ToString()));
        }

        protected void SetSumma(object rehabEstimate, object miscCost)
        {
            /*double re = 0;
            double misc = 0;
            
            if (rehabEstimate != null)
                if (rehabEstimate != System.DBNull.Value)
                    re = Convert.ToDouble(rehabEstimate);
            if (miscCost != null)
                if (miscCost != System.DBNull.Value)
                    misc = Convert.ToDouble(miscCost);

            lblSumma.Text = (re + misc).ToString("c");*/
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.DataBinding += new EventHandler(RehabEditControl_DataBinding);
        }

        void RehabEditControl_DataBinding(object sender, EventArgs e)
        {
          User u = Session["User"] as User;

          FillRehabStatus();
          FillPM();

          edNotesStatus.Enabled = (u.UserRole == UserRole.SuperAdmin);

            string pmId = Eval("PMId").ToString();
            if (edProjectManager.Items.FindItemByValue(pmId) != null)
            {
                edProjectManager.SelectedValue = pmId;
            }
            SetSumma(DataBinder.Eval(DataItem, "RehabEstimate"), DataBinder.Eval(DataItem, "RehabMiscCosts"));

            /*object closeDate = DataBinder.Eval(DataItem, "ECOE_F");
            if (closeDate == System.DBNull.Value || closeDate.ToString() == "")
            {
                edListingAgreement.Enabled = false;
                edContractDate.Enabled = false;
                edBuyerSign.Enabled = false;
                edECOE_B.Enabled = false;
                edSellerSign.Enabled = false;
                edRecorded_B.Enabled = false;
                edDocsIn.Enabled = false;
                edBuyerPurchasePrice.Enabled = false;
                edWalkThrough.Enabled = false;
                edPurchaseType.Enabled = false;
                edLender.Enabled = false;
                edIncentive.Enabled = false;
                edCOOPAgent.Enabled = false;
                edHOAManagement.Enabled = false;
                edBuyerName.Enabled = false;
                edMonthlyHOADues.Enabled = false;
                edTitleCompanyBack.Enabled = false;
                edPastDueHOA.Enabled = false;
                edResalePkgToAgent.Enabled = false;
                edResalePacketOrdered.Enabled = false;
                edCommentsBack.Disabled = true;
            }*/
        }

        object _dataItem = null;

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;

                if (value != null)
                {
                    DataRowView drv = (DataRowView)value;
                    if (drv["PMId"] != DBNull.Value)
                        this.PMIdOld = Convert.ToInt32(drv["PMId"]);
                    else
                        this.PMIdOld = -1;
                }

            }
        }

        public int PMIdOld
        {
            get
            {
                if (string.IsNullOrEmpty(hfPMldOld.Value))
                    return -1;

                return Convert.ToInt32(hfPMldOld.Value);
            }
            set
            {
                hfPMldOld.Value = value.ToString();
            }
        }

     }
}